package com.qq2008.game.stzb.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qq2008.game.stzb.jdo.Letter;
import com.qq2008.game.stzb.jdo.Role;
import com.qq2008.game.stzb.mapper.LetterMapper;
import com.qq2008.game.stzb.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/mojie")
@Slf4j
public class LetterController extends BaseController {

    @Resource
    private LetterMapper letterMapper;

    @Resource
    private RedisUtil redisUtil;

    /**
     * @param getId
     * @param model
     * @param session
     * @return 发信
     */
    @GetMapping("sendLetter")
    public String sendLetter(String getId, Model model, HttpSession session) {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
        String format = df.format(new Date());
        model.addAttribute("timeNow", format);
        model.addAttribute("getId", getId);
        return "sendLetter";
    }


    /**
     * 私信入库
     *
     * @param getId
     * @param talk
     * @param model
     * @param session
     * @return
     */
    @GetMapping("getLetter")
    @ResponseBody
    public String getLetter(String getId, String talk, Model model, HttpSession session) {
        Role role = getSessionRole(session);
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
        String format = df.format(new Date());
        model.addAttribute("timeNow", format);
        Letter letter = Letter.builder()
                .content(talk)
                .getId(Long.parseLong(getId))
                .sendId((long) role.getUserId())
                .sendName(role.getName())
                .build();
        letterMapper.insert(letter);
        return "yes";
    }

    /**
     * 查看私信
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("look")
    public String look(String id, Model model) {
        List<Letter> letters = letterMapper.selectList(new LambdaQueryWrapper<Letter>().eq(Letter::getGetId, id).orderByDesc(Letter::getCreateTime));

        Letter letter = new Letter().builder()
                .isRead(1)
                .getId(Long.parseLong(id))
                .build();
        letterMapper.update(letter, new LambdaQueryWrapper<Letter>().eq(Letter::getGetId, id));
        CommonTime(letters);
        model.addAttribute("letterList", letters);
        commonTime(model);
        return "letter";
    }

    /**
     * @param replyId 回复给玩家的id
     * @return 回复私信页面
     */
    @GetMapping("replySite")
    public String replySite(String replyId, Model model, HttpSession session) {
        Role role = getSessionRole(session);
        model.addAttribute("replyId", replyId);
        LambdaQueryWrapper<Letter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Letter::getGetId, role.getUserId());
        queryWrapper.eq(Letter::getSendId, replyId);
        List<Letter> letters = letterMapper.selectList(queryWrapper);
        CommonTime(letters);
        model.addAttribute("letterList", letters);
        commonTime(model);
        return "replyLetter";
    }

    /**
     * @return 回复私信页面
     */
    @PostMapping("replyLetter")
    public String replyLetter(Model model, HttpSession session, String talk, String replyId) {
        Role role = getSessionRole(session);
        Letter letter = Letter.builder()
                .content(talk)
                .getId(Long.parseLong(replyId))
                .sendId((long) role.getUserId())
                .sendName(role.getName())
                .build();
        letterMapper.insert(letter);

        List<Letter> letters = letterMapper.selectList(new LambdaQueryWrapper<Letter>().eq(Letter::getGetId, role.getUserId()));
        CommonTime(letters);
        model.addAttribute("letterList", letters);
        commonTime(model);
        //提醒私信来了,缓存10分钟
        redisUtil.set("letter", replyId);
        return "letter";
    }

    void CommonTime(List<Letter> letters) {
        for (Letter letter : letters) {
            String formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(letter.getCreateTime());
            letter.setShowTime(formatDate);
        }
    }


    Model commonTime(Model model) {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
        String format = df.format(new Date());
        model.addAttribute("timeNow", format);
        return model;
    }

}
